#ifndef RFILEMODEL_H
#define RFILEMODEL_H

#include <QObject>
#include <QVariant>
#include <QByteArray>
#include <QAbstractListModel>
#include <QTextCodec>
#include <QStringList>

//Forward declare, could increase the compile speed
class QFile;
class REngine;

class RFileModel : public QAbstractListModel
{
    Q_OBJECT
public:
    explicit RFileModel(REngine& engine, QObject *parent = 0);
    virtual ~RFileModel();

    //Open file
    bool setFile(const QString& fileName);

    //Reload file
    void reset();

    //File Name
    QString fileName();

    //File Id
    int fileId();
    //Set file id
    void setFileId(int fileId);

    //Get line number of percent
    int lineNumberOfPercent(float percent);

    //Percent of one line in total lines
    float lineNumberToPercent(int lineNum);

    //Current file codec
    QString fileCodec();

    //Open file result
    int openFileResult();

    //Set open file result
    void setOpenFileResult(int result);

    //***********Chapter*****************************
    //Line number of chapter
    virtual int lineNumberOfChapter(int chapterIndex) = 0;
    //Current chapter index
    virtual int currentChapterIndex() = 0;
    //Chapters of file
    virtual void chapters(QStringList& chapterList) = 0;

public: //From QAbstractListModel
    int rowCount(const QModelIndex & parent = QModelIndex()) const;
    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
    virtual QVariant data(int row) const  = 0;

protected:
    virtual bool analyzeFile() = 0;
    virtual bool openFile(const QString& fileName) = 0;
    virtual bool saveFileLinePosition() = 0;
    virtual bool readFileLinePosition() = 0;
    virtual bool initFileInfo();
    int newLinePosPadding();
    int newLineCode(const QTextCodec& codec);
    bool newLineCharIsLittleEndian(const QTextCodec& codec);
    bool lastCharIsNewLine(const QByteArray & array,
                           const QTextCodec & codec);
    int newLineCharLaw(const QTextCodec& codec, int& fetchMore);

private:


signals:

public slots:

protected:
    REngine& m_engine;
    QTextCodec* m_codec;
    QFile* m_file;
    QHash<int, qint64> m_file_line_pos;
    QStringList m_file_chapter;
    int m_file_id;
    int m_open_result;
};

#endif // RFILEMODEL_H
